package com.xiaoyu.lanling.feature.invite.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.event.invite.OtherWithdrawEvent
import com.xiaoyu.lanling.event.invite.list.InviteEarnInfoEvent
import com.xiaoyu.lanling.feature.invite.controller.InviteEarningController
import com.xiaoyu.lanling.feature.invite.data.InviteData
import com.xiaoyu.lanling.feature.invite.data.InviteEarningListData
import com.xiaoyu.lanling.feature.invite.dialog.InviteShareDialog
import com.xiaoyu.lanling.util.TextViewUtil
import com.xiaoyu.lanling.view.OnShowFragment
import kotlinx.android.synthetic.main.fragment_invite_earning_detail.*
import kotlinx.android.synthetic.main.invite_detail_summary_header.*
import kotlinx.android.synthetic.main.invite_empty_layout.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class InviteEarningDetailFragment : OnShowFragment() {
    private var listController: InviteEarningController? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_invite_earning_detail, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        initBind()
        initEvent()
        initData()
    }

    private fun initBind() {
        invite_encourage_button.setOnClickListener {
            activity?.let {
                InviteShareDialog.show(childFragmentManager)
            }
        }
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: InviteEarnInfoEvent) {
                today_earning.text = event.todayMoney
                all_earning.text = event.totalMoney
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: OtherWithdrawEvent) {
                TextViewUtil.stripUnderlinesAndClickable(withdraw_message, event.message)
            }
        })
    }

    private fun initData() {
        activity?.let {
            val listData = InviteEarningListData()
            listController = InviteEarningController.create(it, listData, swipe_refresh_layout, recyclerview)
            listController?.setEmptyCallback {
                invite_empty_layout.visibility = View.VISIBLE
                activity?.let {
                    val scaleAnimation = ScaleAnimation(0.85f, 1f, 0.85f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
                    scaleAnimation.repeatMode = Animation.REVERSE
                    scaleAnimation.repeatCount = Animation.INFINITE
                    scaleAnimation.duration = 500
                    invite_encourage_button.startAnimation(scaleAnimation)
                }
            }
            listController?.setNotEmptyCallback {
                invite_empty_layout.visibility = View.GONE
            }
            listController?.loadFirstPageData()
            InviteData.getInviteEarnInfo(listData.requestTag)
        }
    }
}